#include "h_stm32_hal_spi.h"

#include <stdio.h>
#include <string.h>

#include "h_stm32_hal_defines.h"

extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi2;
#define H_STM32_HAL_SPI1                hspi1
#define H_STM32_HAL_SPI2                hspi2

#define H_STM32_HAL_SPI1_CS_PORT        GPIOB
#define H_STM32_HAL_SPI1_CS_PIN         GPIO_PIN_5

#define H_STM32_HAL_SPI2_CS_PORT        GPIOB
#define H_STM32_HAL_SPI2_CS_PIN         GPIO_PIN_9

void h_stm32_hal_spi1_cs_enable(void)
{
    HAL_GPIO_WritePin(H_STM32_HAL_SPI1_CS_PORT, H_STM32_HAL_SPI1_CS_PIN, GPIO_PIN_RESET);
}

void h_stm32_hal_spi1_cs_disable(void)
{
    HAL_GPIO_WritePin(H_STM32_HAL_SPI1_CS_PORT, H_STM32_HAL_SPI1_CS_PIN, GPIO_PIN_SET);
}

h_stm32_hal_spi_state h_stm32_hal_spi1_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = H_STM32_HAL_SPI1_CS_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(H_STM32_HAL_SPI1_CS_PORT, &GPIO_InitStruct);

    h_stm32_hal_spi1_cs_disable();

    return h_stm32_hal_spi_ok;
}

h_stm32_hal_spi_state h_stm32_hal_spi1_write(unsigned char *data, unsigned short len)
{
    return (h_stm32_hal_spi_state)HAL_SPI_Transmit(&H_STM32_HAL_SPI1, data, len, 0xFFFF);
}

h_stm32_hal_spi_state h_stm32_hal_spi1_read(unsigned char *data, unsigned short len)
{
    return (h_stm32_hal_spi_state)HAL_SPI_Receive(&H_STM32_HAL_SPI1, data, len, 0xFFFF);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void h_stm32_hal_spi2_cs_enable(void)
{
    HAL_GPIO_WritePin(H_STM32_HAL_SPI2_CS_PORT, H_STM32_HAL_SPI2_CS_PIN, GPIO_PIN_RESET);
}

void h_stm32_hal_spi2_cs_disable(void)
{
    HAL_GPIO_WritePin(H_STM32_HAL_SPI2_CS_PORT, H_STM32_HAL_SPI2_CS_PIN, GPIO_PIN_SET);
}

h_stm32_hal_spi_state h_stm32_hal_spi2_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = H_STM32_HAL_SPI2_CS_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(H_STM32_HAL_SPI2_CS_PORT, &GPIO_InitStruct);

    h_stm32_hal_spi2_cs_disable();

    return h_stm32_hal_spi_ok;
}

h_stm32_hal_spi_state h_stm32_hal_spi2_write(unsigned char *data, unsigned short len)
{
    return (h_stm32_hal_spi_state)HAL_SPI_Transmit(&H_STM32_HAL_SPI2, data, len, 0xFFFF);
}

h_stm32_hal_spi_state h_stm32_hal_spi2_read(unsigned char *data, unsigned short len)
{
    return (h_stm32_hal_spi_state)HAL_SPI_Receive(&H_STM32_HAL_SPI2, data, len, 0xFFFF);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char ttdata[] = {0xD0, 0x08, 0x05, 0x06};

h_stm32_hal_spi_state h_stm32_hal_spi_test(void)
{
    h_stm32_hal_spi2_cs_enable();
    h_stm32_hal_spi2_write(ttdata, 1);
    h_stm32_hal_spi2_read(ttdata, 2);
    h_stm32_hal_spi2_cs_disable();

    printf("0x%02x%02x\r\n", ttdata[0], ttdata[1]);
    return h_stm32_hal_spi_ok;
}

